package user_center.menu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import user_center.menu.mapper.SystemMenuMapper;
import user_center.menu.pojo.SystemMenu;
import user_center.menu.pojo.SystemMenuRole;
import user_center.role.mapper.SystemRoleUserMapper;
import user_center.role.pojo.SystemRoleUser;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ezio.Auditore
 * @since 2021-12-20
 */
@Service
public class SystemMenuServiceImpl extends ServiceImpl<SystemMenuMapper, SystemMenu> {

    @Autowired
    SystemMenuMapper systemMenuMapper;

    @Autowired
    SystemRoleUserMapper systemRoleUserMapper;


    public List<SystemMenuRole> getMenuRoleList(Integer userId) {
        List<SystemMenuRole> menus = new ArrayList<>();
        List<SystemRoleUser> systemRoleUsers = systemRoleUserMapper.selectRolesByUserId(userId);
        Set<Integer> collect = systemRoleUsers.stream().map(item -> item.getRoleId()).collect(Collectors.toSet());
        for (Integer roleId : collect) {
            menus.addAll(systemMenuMapper.getMenuRoleList(roleId));
        }
        return menus;
    }

    /**
     * 新增菜单
     *
     * @param systemMenuList
     * @return
     */
    public boolean addMenu(List<SystemMenu> systemMenuList) {
        try {
            systemMenuList.stream().map(item -> systemMenuMapper.insert(item));
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
